home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 42 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.8 KB  |  57 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.std.c
  4. Subject: Re: Alignment of malloc()
  5. Date: Sat, 06 Jan 96 18:29:36 GMT
  6. Organization: none
  7. Message-ID: <820952976snz@genesis.demon.co.uk>
  8. References: <DKDA7D.Kw7@midway.uchicago.edu> <j66Sx*FRe@yaps.rhein.de> <DKKHCH.L6r@midway.uchicago.edu> <4ccbdb$5v6@fg70.rz.uni-karlsruhe.de> <DKo8ns.8A9@midway.uchicago.edu> <DKp9L9.234@ukpsshp1.serigate.philips.nl>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <DKp9L9.234@ukpsshp1.serigate.philips.nl>
  15.            baynes@ukpsshp1.serigate.philips.nl "Stephen Baynes" writes:
  16.  
  17. >This reminds me of a discussion over could calloc use the size parameter
  18. >as a hint to give different alignment to malloc. For example:
  19. >    calloc( N, sizeof( float ) )
  20. >could recognize the sizeof float and give 64 bit alignment in this case.
  21. >I recall the conclusion was that it could do this, but did not have to.
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25.  
  26. #define NELEMS 500
  27.  
  28. int main(void)
  29.  
  30. {
  31.     long *squares = calloc(NELEMS * sizeof(long), 1);
  32.  
  33.     if (squares != NULL) {
  34.         int ind;
  35.  
  36.         for (ind = 0; ind < NELEMS; ind++)
  37.             squares[ind] = (long)ind * ind;
  38.  
  39.         for (ind = 0; ind < NELEMS; ind++)
  40.             printf("%ld\n", squares[ind]);
  41.  
  42.         free(squares);
  43.     }
  44.  
  45.     return 0;
  46. }
  47.  
  48. A conforming implementation must compile and run this program correctly.
  49. Of course it could use such a 'hint' where the only result of sub-optimal
  50. alignment is a performance hit.
  51.  
  52. -- 
  53. -----------------------------------------
  54. Lawrence Kirby | fred@genesis.demon.co.uk
  55. Wilts, England | 70734.126@compuserve.com
  56. -----------------------------------------
  57.